﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;
using TarreOA.TarreOADAL;

namespace TarreOA.TarreOABLL
{
    public class IndexMain_Service
    {
        private SqlConnection sqlConn = null;

        public Hashtable FindIndexInfo(User curLoginUser)
        {
            Hashtable ht = new Hashtable();
            sqlConn = SQLManager.GetConn();
            try
            {
                if(sqlConn != null)
                {
                    sqlConn.Open();
                }
                ht.Add("AFFICHEINFORM", this.FindAfficheInform(curLoginUser.Section.SectionId));
                ht.Add("USERDIARY", this.FindUserDiary(curLoginUser.LoginName));
                ht.Add("CURRENTCALENDAR", this.FindCurrentCalendar(curLoginUser.LoginName));
                ht.Add("NETDISK", this.FindNetDisk(curLoginUser.Section.SectionId));
                ht.Add("NEWS", this.FindNews());
                ht.Add("TALKHOUSE", this.FindTalkHouse());
            }
            finally
            {
                if(sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return ht;
        }

        /// <summary>
        /// 查找公告通知
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindAfficheInform(int usid)
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from AfficheInformtb where AfficheInformEndTime > @now and (AfficheInformOpenBound like @sid or AfficheInformOpenBound = '0') order by AfficheInformCreateTime desc";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@now", DateTime.Now.Date));
            sqlCmd.Parameters.Add(new SqlParameter("@sid", "%," + usid + ",%"));
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='AfficheInform/AfficheInformInfo.aspx?ai_id=" + sqlDr["AfficheInformId"].ToString() +
                            "'>" + sqlDr["AfficheInformTitle"].ToString() + "</a><span class='riqi'>[" + ((DateTime)sqlDr["AfficheInformCreateTime"]).ToShortDateString() + "]</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>暂无公告通知</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }

        /// <summary>
        /// 查找工作计划
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindUserDiary(string uln)
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from UserDiarytb where UserLoginName = @uln order by RegisterTime desc";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@uln", uln));
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows && false)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='User/UserDiary/DiaryContent.aspx?did=" + sqlDr["DiaryId"].ToString() +
                            "'>" + sqlDr["DiaryTitle"] + "</a><span class='riqi'>"+ ((DateTime)sqlDr["RegisterTime"]).ToShortDateString()+"</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>暂无工作计划</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }

        /// <summary>
        /// 查找今日日程
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindCurrentCalendar(string uln)
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from UserCalendarArrangetb where UserLoginName = @uln and CalendarArrangeDate = @curDate";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@uln", uln));
            sqlCmd.Parameters.Add(new SqlParameter("@curDate", DateTime.Parse(DateTime.Now.ToShortDateString())));
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='CalendarArrange/CalendarEdit.aspx?ca_id=" + sqlDr["CalendarArrangeId"].ToString() +
                            "&c_dt=" + DateTime.Now.ToShortDateString() + "'>" + sqlDr["CalendarArrangeTitle"] + "</a><span class='riqi'>["+DateTime.Now.ToShortDateString()+"]</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>今日无日程安排</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }

        /// <summary>
        /// 查找网络硬盘
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindNetDisk(int usid)
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from NetDisk where NetDiskCatalogOpenBound like '@usid,%' or NetDiskCatalogOpenBound like '%,@usid,%' or NetDiskCatalogOpenBound like '0,'";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.Parameters.Add(new SqlParameter("@usid", usid));
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='NetDisk/NetDiskCatalogFolderShow.aspx?C_N=" + sqlDr["NetDiskCatalogName"] + "&C_ID=" + sqlDr["NetDiskCatalogId"].ToString() +
                            "&c_dt=" + DateTime.Now.ToShortDateString() + "'>" + sqlDr["NetDiskCatalogName"] + "</a><span class='riqi'>["+((DateTime)sqlDr["NetDiskCreateTime"]).ToShortDateString()+"]</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>暂无对您开放的共享目录</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }

        /// <summary>
        /// 查找聊天室
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindTalkHouse()
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from TalkHouseList where TalkHouseOpenState = 0";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='UserTalkHouse/InTalk.aspx?thl_Id=" + sqlDr["TalkHouseId"] + "'>" 
                            + sqlDr["TalkHouseName"] + "</a><span class='riqi'>["+((DateTime)sqlDr["TalkHouseLastUpTime"]).ToShortDateString()+"]</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>暂无开放聊天室</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }

        /// <summary>
        /// 查找新闻
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        private StringBuilder FindNews()
        {
            SqlDataReader sqlDr = null;
            StringBuilder strbu = new StringBuilder();
            string sql = "select top 5 * from News order by newsdate desc";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            try
            {
                sqlDr = sqlCmd.ExecuteReader();
                if(sqlDr.HasRows)
                {
                    while(sqlDr.Read())
                    {
                        strbu.Append("<li><a href='page/ShowDetail.aspx?id=" + sqlDr["id"] + "'>" + sqlDr["Title"]
                            + "</a><span class='riqi'>[" + ((DateTime)sqlDr["NewsDate"]).ToShortDateString() + "]</span></li>");
                    }
                }
                else
                {
                    strbu.Append("<li>暂无新闻</li>");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            return strbu;
        }
    }
}
